package com.mac.service;

import com.mac.mapper.ResearchResultsMapper;
import com.mac.pojo.ResearchResults;
import com.mac.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;

public class ResearchResultsService implements ResearchResultsMapper {

    // 定义日志对象
    private static final Logger LOGGER = LoggerFactory.getLogger("ResearchResultsService.Class");

    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

@SuppressWarnings({"all"})
    @Override
    public ArrayList<ResearchResults> selectAllResearch() {
        SqlSession sqlSession = factory.openSession();

        ResearchResultsMapper researchResultsMapper = sqlSession.getMapper(ResearchResultsMapper.class);

        ArrayList<ResearchResults> researchResults;
        try {
            researchResults = researchResultsMapper.selectAllResearch();
            sqlSession.close();
            LOGGER.info("selectAllResearch() -> succeed",researchResults);
            return researchResults;
        } catch (Exception e) {
            sqlSession.close();
            LOGGER.error("selectAllResearch() -> failed",e);
            return null;
        }
    }

    @Override
    public ResearchResults selectOneByResultId(int resultId) {
        SqlSession sqlSession = factory.openSession();

        ResearchResultsMapper researchResultsMapper = sqlSession.getMapper(ResearchResultsMapper.class);

        ResearchResults researchResult;
        try {
            researchResult = researchResultsMapper.selectOneByResultId(resultId);
            sqlSession.close();
            LOGGER.info("selectOneByResultId(resultId) -> succeed",researchResult);
            return researchResult;
        } catch (Exception e) {
            sqlSession.close();
            LOGGER.error("selectOneByResultId(resultId) -> failed",e);
            return null;
        }
    }
}
